//
//  MainProductCell.m
//  Peso Guide
//
//  Created by macbook on 2025/6/10.
//

#import "MainProductCell.h"
#import "CustomWebViewController.h"
#import "MineSettingViewController.h"
#import "ProductDetailViewController.h"

@interface MainProductCell ()

@property (nonatomic, strong) UIView *backView;
//@property (nonatomic, strong) UILabel *leftPriceLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *titleLabel1;
@property (nonatomic, strong) UILabel *titleContentLabel1;
@property (nonatomic, strong) UILabel *titleLabel2;
@property (nonatomic, strong) UILabel *titleContentLabel2;
@property (nonatomic, strong) UIImageView *iconImg;
@property (nonatomic, strong) UILabel *iconNameLabel;
@property (nonatomic, strong) UIButton *applyButton;

@end

@implementation MainProductCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        
        _backView = [PesoKit createViewWithFrame:CGRectMake(16, 16, KScreenWidth-32, 95) backGroundColor:ColorHex(@"#CCFFD1")];
        _backView.clipsToBounds = YES;
        _backView.layer.cornerRadius = 15.0f;
        [self.contentView addSubview:_backView];
        
//        _leftPriceLabel = [PesoKit createLabelWithFrame:CGRectMake(17, 21, 10, 22) backGroundColor:ColorHex(@"#CCFFD1") text:@"₱" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 16)];
//        _leftPriceLabel.textAlignment = NSTextAlignmentCenter;
//        [_backView addSubview:_leftPriceLabel];
        
        _priceLabel = [PesoKit createLabelWithFrame:CGRectMake(17, 14, 109, 39) backGroundColor:ColorHex(@"#CCFFD1") text:@"250,000" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleMedium, 27)];
        
//        _titleLabel1 = [PesoKit createLabelWithFrame:CGRectMake(_backView.width-20-80-20-80-16, 10, 80, 20) backGroundColor:ColorHex(@"#CCFFD1") text:@"" textColor:ColorHex(@"#999999") font:FONT(FontWeightStyleRegular, 14)];
//        [_backView addSubview:_titleLabel1];
        
        _titleContentLabel1 = [PesoKit createLabelWithFrame:CGRectMake(_titleLabel1.left, _titleLabel1.bottom, _titleLabel1.width, 20) backGroundColor:ColorHex(@"#CCFFD1") text:@"" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 14)];
        _titleContentLabel1.textAlignment = NSTextAlignmentRight;
        [_backView addSubview:_titleContentLabel1];
        
//        _titleLabel2 = [PesoKit createLabelWithFrame:CGRectMake(_backView.width-20-80, 10, 80, 20) backGroundColor:ColorHex(@"#CCFFD1") text:@"" textColor:ColorHex(@"#999999") font:FONT(FontWeightStyleRegular, 14)];
//        [_backView addSubview:_titleLabel2];
        
        _titleContentLabel2 = [PesoKit createLabelWithFrame:CGRectMake(_titleLabel2.left, _titleLabel2.bottom, _titleLabel2.width, 20) backGroundColor:ColorHex(@"#CCFFD1") text:@"" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 14)];
        _titleContentLabel2.textAlignment = NSTextAlignmentCenter;
        [_backView addSubview:_titleContentLabel2];
        
        _iconImg = [PesoKit createImageViewWithFrame:CGRectMake(16, 61, 20, 20) imageNamed:@""];
        _iconImg.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
        [_backView addSubview:_iconImg];
        
        _iconNameLabel = [PesoKit createLabelWithFrame:CGRectMake(_iconImg.right+5, 61, 200, 20) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 14)];
        [_backView addSubview:_iconNameLabel];
        
        _applyButton = [PesoKit createButtonWithFrame:CGRectMake(_backView.width-16-105, 58, 105, 26) backGroundColor:ColorHex(@"#FE7133") title:@"" titleColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleRegular, 14)];
        _applyButton.clipsToBounds = YES;
        _applyButton.layer.cornerRadius = 13.0f;
        [_applyButton addTarget:self action:@selector(applyButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_backView addSubview:_applyButton];
        
        [_backView addSubview:_priceLabel];
    }
    return self;
}

- (void)setDataDic:(NSDictionary *)dataDic {
    _dataDic = dataDic;
    
    NSString *allowed = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"allowed"]];
    NSString *follows = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"follows"]];
    NSString *basis = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"basis"]];
    NSString *raises = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"raises"]];
    NSString *per = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"per"]];
    NSString *optimization = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"optimization"]];
    NSString *raised = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"raised"]];
    NSString *assigning = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"assigning"]];

    _priceLabel.text = follows;
    CGSize pricceSize = [AppTools sizeWithText:follows font:FONT(FontWeightStyleMedium, 27) maxSize:CGSizeMake(KScreenWidth-32, 39)];
    _priceLabel.width = ceil(pricceSize.width)+5;
    
    _titleLabel2.text = per;
    _titleContentLabel2.text = optimization;
    
    CGSize title2Size = [AppTools sizeWithText:per font:FONT(FontWeightStyleRegular, 14) maxSize:CGSizeMake(KScreenWidth-32, 20)];
    if (title2Size.width < 80) {
        _titleLabel2.frame = CGRectMake(_backView.width-16-80, 10, 80, 20);
    }else {
        _titleLabel2.frame = CGRectMake(_backView.width-16-ceil(title2Size.width), 10, ceil(title2Size.width), 20);
    }
    
    CGSize title2ContentSize = [AppTools sizeWithText:optimization font:FONT(FontWeightStyleRegular, 14) maxSize:CGSizeMake(KScreenWidth-32, 20)];
    CGFloat title2Width = 0;
    
    if (title2ContentSize.width < 80) {
        title2Width = 80;
        _titleContentLabel2.frame = CGRectMake(_backView.width-16-80, 10, 80, 20);
    }else {
        title2Width = ceil(title2ContentSize.width);
        _titleContentLabel2.frame = CGRectMake(_backView.width-16-ceil(title2ContentSize.width), _titleLabel1.bottom, ceil(title2ContentSize.width), 20);
    }
    
//    _titleLabel1.text = basis;
    _titleContentLabel1.text = raises;
    
    CGSize title1Size = [AppTools sizeWithText:basis font:FONT(FontWeightStyleRegular, 14) maxSize:CGSizeMake(KScreenWidth-32, 20)];
    if (title1Size.width < 80) {
        _titleLabel1.frame = CGRectMake(_backView.width-16-title2Width-10-80, 10, 80, 20);
    }else {
        _titleLabel1.frame = CGRectMake(_backView.width-16-title2Width-10-ceil(title1Size.width), 10, ceil(title1Size.width), 20);
    }
    
    CGSize title1ContentSize = [AppTools sizeWithText:basis font:FONT(FontWeightStyleRegular, 14) maxSize:CGSizeMake(KScreenWidth-32, 20)];
    if (title1ContentSize.width < 80) {
        _titleContentLabel1.frame = CGRectMake(_backView.width-16-title2Width-10-80, 10, 80, 20);
    }else {
        _titleContentLabel1.frame = CGRectMake(_backView.width-16-title2Width-10-ceil(title1ContentSize.width), 10, ceil(title1ContentSize.width), 20);
    }
    
    [_iconImg sd_setImageWithURL:[NSURL URLWithString:raised]];
    _iconNameLabel.text = allowed;
    [_applyButton setTitle:assigning forState:0];
}

- (void)applyButtonClick {
    DLog(@"shenqing");
    kPreventRepeatClickTime(1.5);

    if ([AppTools isLogin] == NO) {
        [AppTools toLogin];
        return;
    }
    
    NSDictionary *dataDic = [NSDictionary safeDictionaryWithDictionary:self.dataDic];
    NSString *causes = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"causes"]];
    
    [[AWHttpService service] postWithUrl:@"/trains/synonymous" params:@{@"across":causes} succ:^(id  _Nonnull responseObject) {
        if (SUCCESS) {
            NSDictionary *associating = [NSDictionary safeDictionaryWithDictionary:[responseObject safeObjectForKey:@"associating"]];
            NSString *recent = [NSString stringWithFormat:@"%@",[associating safeObjectForKey:@"recent"]];
            
            if ([recent rangeOfString:@"http://"].location != NSNotFound ||
                [recent rangeOfString:@"https://"].location != NSNotFound) {
                //webView
                CustomWebViewController *webVC = [[CustomWebViewController alloc] init];
                webVC.linkUrl = recent;
                [[AppTools getCurrentVC].navigationController pushViewController:webVC animated:YES];
                return;
            }
            if ([recent rangeOfString:@"pe://so.gui.de/thelike"].location != NSNotFound) {
                //设置
                MineSettingViewController *settingVC = [[MineSettingViewController alloc] init];
                settingVC.hidesBottomBarWhenPushed = YES;
                [[AppTools getCurrentVC].navigationController pushViewController:settingVC animated:YES];
                return;
                return;
            }
            if ([recent rangeOfString:@"pe://so.gui.de/andlove"].location != NSNotFound) {
                //首页
                [AppTools getCurrentVC].tabBarController.selectedIndex = 0;
                return;
            }
            if ([recent rangeOfString:@"pe://so.gui.de/standard"].location != NSNotFound) {
                //登录
                [AppTools toLogin];
                return;
            }
            if ([recent rangeOfString:@"pe://so.gui.de/classes"].location != NSNotFound) {
                //订单
                [AppTools getCurrentVC].tabBarController.selectedIndex = 1;
                return;
            }
            if ([recent rangeOfString:@"pe://so.gui.de/islove"].location != NSNotFound) {
                //产品详情
                NSArray *array = [recent componentsSeparatedByString:@"="];
                NSString *theId = @"";
                if (array.count == 2) {
                    theId = [NSString stringWithFormat:@"%@",[array safeObjectAtIndex:1]];
                }else {
                    theId = causes;
                }
                ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
                vc.productId = theId;
                vc.hidesBottomBarWhenPushed = YES;
                [[AppTools getCurrentVC].navigationController pushViewController:vc animated:YES];
                return;
            }
        }else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",[responseObject safeObjectForKey:@"order"]]];
            [SVProgressHUD dismissWithDelay:3.0f];

        }
    } fail:^(NSInteger errorType, id  _Nonnull error) {}];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
